<?php
/**
 * Created by JetBrains PhpStorm.
 * User: heimuya
 * Date: 14-1-26
 * Time: 下午1:33
 * 系统设置控制器类
 */
class SystemAction extends AdminAction{
	
	public function _initialize(){
		parent::_initialize();
		$this->assign('page', __CLASS__);
	}
	
    /**
     * 地区管理
     */
    public function region(){
    	$regionList = D("Region")->select();//获取分类信息
	 	
		$this->assign('method', 'regionoption');
	 	$this->assign('regionlist', $regionList);
        $this->display();
    }
	
	
    /**
     * 群组管理
     */
    public function group(){
    	$groupList = D("Group")->select();//获取分类信息
	 	
		$this->assign('method', 'groupoption');
	 	$this->assign('grouplist', $groupList);
        $this->display();
    }
	
    /**
     * 总部管理
     */
    public function headoffice(){
    	$headofficeList = D("Headoffice")->select();//获取分类信息
	 	
		$this->assign('method', 'headofficeoption');
	 	$this->assign('headofficelist', $headofficeList);
        $this->display();
    }
	
    /**
     * 门店管理
     */
    public function store(){
    	$storeList = D("Store")->select();//获取分类信息
	 	$regionList = changeArrayKey(D("Region")->field('id,name')->select());
		$groupList = changeArrayKey(D("Group")->field('id,name')->select());
		$headofficeList = changeArrayKey(D("Headoffice")->field('id,name')->select());
		
		foreach ($storeList as $key => $value) {
			$storeList[$key]['region'] = $regionList[$storeList[$key]['region']];
			$storeList[$key]['group'] = $groupList[$storeList[$key]['group']];
			$storeList[$key]['headoffice'] = $headofficeList[$storeList[$key]['headoffice']];
		}
	 	
		$this->assign('method', 'storeoption');
	 	$this->assign('storelist', $storeList);
		$this->assign('regionlist', $regionList);
		$this->assign('grouplist', $groupList);
		$this->assign('headofficelist', $headofficeList);
        $this->display();
    }
	
	/**
	 * 用户管理
	 */
	 public function usermanage(){
	 	$storeList = changeArrayKey(D("Store")->field('id,name')->select());
		$this->assign('storelist', $storeList);
		$this->assign('method', 'usermanage');
	 	$this->display();
	 }
	 
	/**
	 * 异步获取用户数据
	 */
	 public function userjson(){
	 	$aColumns = array('id', 'name', 'nick', 'last_login', 'type', 'valid', 'act');
	 	$getdata = $this->_get();
		
		$order = "";
		if (isset($getdata['iSortCol_0'])){
			for ($i=0 ; $i<intval( $getdata['iSortingCols'] ) ; $i++){
				if ($getdata[ 'bSortable_'.intval($getdata['iSortCol_'.$i]) ] == "true"){
					if($aColumns[intval( $getdata['iSortCol_'.$i] ) ] == 'act' || $aColumns[intval( $getdata['iSortCol_'.$i] ) ] == 'nick' || $aColumns[intval( $getdata['iSortCol_'.$i] ) ] == 'name'){
						continue;
					}
					$order .= "`".$aColumns[intval( $getdata['iSortCol_'.$i] ) ]."` ".
						($getdata['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
				}
			}
			$order = substr_replace( $order, "", -2 );
	 	}
		$map = array();
		if (isset($getdata['sSearch']) && $getdata['sSearch'] != ""){
			$where['name'] = array('like', '%'.$getdata['sSearch'].'%');
			$where['nick'] = array('like', '%'.$getdata['sSearch'].'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
		
		
		$result = $this->pageList_dataTable(D('User'), $getdata, $map, 'password', $order);
		
		$output = array(
			'sEcho'					=> intval($getdata['sEcho']),
			'iTotalRecords'			=> D('User')->count(),//除掉当前登录用户自己
			'iTotalDisplayRecords'	=> $result['count'],
			'aaData'				=> array()
		);
		
		foreach ($result['list'] as $key => $value) {
			$row = array();
			for($i = 0; $i < count($aColumns); $i++){
				if($aColumns[$i] == 'type'){
					$row[] = '<input type="hidden" value="'.$value['store'].'" name="store" /><input type="hidden" value="'.$value['type'].'" name="type" />'.($value['type'] == 'admin' ? L('admin') : L('ordinaryuser'));
				}else if($aColumns[$i] == 'act'){
					$row[] = '<a href="#" class="btn blue icn-only"><i class="icon-edit"></i></a>  '.
							'<a href="#" class="btn red icn-only"><i class="icon-remove icon-white"></i></a>';
				}else if($aColumns[$i] == 'last_login'){
					$row[] = date('Y-m-d H:i', $value['last_login']);
				}else if($aColumns[$i] == 'valid'){
					if($value['valid'] === 'n'){
						$row[] = '<input type="hidden" value="'.$value['valid'].'" name="valid" /><span class="red">'.L('no').'</span>';
					}else{
						$row[] = '<input type="hidden" value="'.$value['valid'].'" name="valid" />'.L('yes');
					}
				}else{
					$row[] = $value[$aColumns[$i]];
				}
			}
			$output['aaData'][] = $row;
		}

		echo json_encode($output);
		exit(0);
	 }
		 
	/**
	 * 新增信息
	 */
	 public function create(){
	 	//异步提交的数据
		$postdata = $this->_post();
		switch ($postdata['method']) {
			case 'region':
				$data['name'] = $postdata['regionname'];
				D('Region')->add($data);
				break;
			case 'system':
				$data['sequence'] = $this->checkSequence('Category', $postdata['categorysequence']);
				$data['name'] = $postdata['categoryname'];
				$data['visible'] = $postdata['visible'];
				D('Category')->add($data);
				break;
			case 'group':
				$data['name'] = $postdata['groupname'];
				$data['number'] = $postdata['groupnumber'];
				$data['notes'] = $postdata['remark'];
				D('Group')->add($data);
				break;
			case 'store':
				$data['name'] = $postdata['storename'];
				$data['number'] = $postdata['storenumber'];
				$data['store_tel'] = $postdata['telephone'];
				$data['manager_mob'] = $postdata['mobilephone'];
				$data['region'] = $postdata['region'];
				$data['manager'] = $postdata['manager'];
				$data['headoffice'] = $postdata['headoffice'];
				$data['group'] = $postdata['group'];
				$data['address'] = $postdata['address'];
				$data['type'] = $postdata['storetype'];
				D('Store')->add($data);
				break;
			case 'usermanage':
				if($postdata['password'] !== $postdata['confirmpassword']){
					echo json_encode(array('status' => 201, 'errorMsg' => L('passwordError')));
					exit(0);
				}
				$data['name'] = $postdata['username'];
				$data['nick'] = $postdata['nick'];
				$data['password'] = md5($postdata['password']);
				$data['type'] = $postdata['type'];
				$data['valid'] = $postdata['valid'];
				$data['store'] = $postdata['store'];
				D('User')->add($data);
				break;
			case 'headoffice':
				$data['name'] = $postdata['headofficename'];
				$data['number'] = $postdata['headofficenumber'];
				$data['address'] = $postdata['address'];
				$data['telephone'] = $postdata['telephone'];
				D('Headoffice')->add($data);
				break;
				
			default:
				
				break;
		}
		
		echo json_encode(array('status' => 200));
		exit(0);
	 }
	 
	/**
	 * 编辑信息
	 */
	 public function edit(){
	 	//异步提交的数据
	 	$postdata = $this->_post();
		switch ($postdata['method']) {
			case 'region':
				$data['name'] = $postdata['regionname'];
				$where['id'] = $postdata['regionid'];
				D('Region')->where($where)->save($data);
				break;
			case 'system':
				$data['sequence'] = $this->checkSequence('Category', $postdata['categorysequence'], 'edit', $postdata['categoryid']);
				$data['name'] = $postdata['categoryname'];
				$data['visible'] = $postdata['visible'];
				$where['id'] = $postdata['categoryid'];
				D('Category')->where($where)->save($data);
				break;
			case 'group':
				$data['name'] = $postdata['groupname'];
				$data['number'] = $postdata['groupnumber'];
				$data['notes'] = $postdata['remark'];
				$where['id'] = $postdata['groupid'];
				D('Group')->where($where)->save($data);
				break;
			case 'store':
				$data['name'] = $postdata['storename'];
				$data['number'] = $postdata['storenumber'];
				$data['store_tel'] = $postdata['telephone'];
				$data['manager_mob'] = $postdata['mobilephone'];
				$data['region'] = $postdata['region'];
				$data['manager'] = $postdata['manager'];
				$data['headoffice'] = $postdata['headoffice'];
				$data['group'] = $postdata['group'];
				$data['address'] = $postdata['address'];
				$data['type'] = $postdata['storetype'];
				$where['id'] = $postdata['storeid'];
				D('Store')->where($where)->save($data);
				break;
			case 'usermanage':
				if($postdata['password'] !== $postdata['confirmpassword']){
					echo json_encode(array('status' => 201, 'errorMsg' => L('passwordError')));
					exit(0);
				}
				$data['name'] = $postdata['username'];
				$data['nick'] = $postdata['nick'];
				$data['password'] = md5($postdata['password']);
				$data['type'] = $postdata['type'];
				$data['valid'] = $postdata['valid'];
				$data['store'] = $postdata['store'];
				$where['id'] = $postdata['userid'];
				D('User')->where($where)->save($data);
				break;
			case 'headoffice':
				$data['name'] = $postdata['headofficename'];
				$data['number'] = $postdata['headofficenumber'];
				$data['address'] = $postdata['address'];
				$data['telephone'] = $postdata['telephone'];
				$where['id'] = $postdata['headofficeid'];
				D('Headoffice')->where($where)->save($data);
				break;
				
			default:
				
				break;
		}
		
		echo json_encode(array('status' => 200));
		exit(0);
	 }
	 
	/**
	 * 删除信息
	 */
	 public function delete(){
	 	//异步提交的数据
	 	$trans = array(
			'region'		=> 'Region',
			'system'		=> 'SystemOption',
			'group'			=> 'Group',
			'store'			=> 'Store',
			'usermanage'	=> 'User',
			'headoffice'	=> 'Headoffice'
		);
	 	$postdata = $this->_post();
		
		$where['id'] = $postdata['id'];
		D($trans[$postdata['method']])->where($where)->delete();
		
		echo json_encode(array('status' => 200));
		exit(0);
	 }
}